package controllers

import (
	"CRMSystem/models"
	"encoding/json"
	"fmt"
)

type OrderController struct {
	MainController
}

// GetOrders 获得订单信息
func (c *OrderController) GetOrders() {
	c.IfHasSession()
	serviceID, _ := c.GetInt("serviceID")
	fmt.Println(serviceID)
	orders, i := models.GetOrderListByServiceID(serviceID)
	fmt.Println(orders)
	if i > 0 {
		c.Data["json"] = Status{Date: orders, ErrCode: GET_OK, Msg: "GetOrders success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: GET_ERR, Msg: "GetOrders error"}
		c.ServeJSON()
		c.StopRun()
	}

}

// AddOrders 添加订单信息
func (c *OrderController) AddOrders() {
	c.IfHasSession()
	sub := models.OrderSubmitModel{}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &sub); err == nil {
		if sub.OrderName == "" {
			c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
			c.ServeJSON()
			c.StopRun()
		}
		fmt.Println("asdasdas", sub)
		err = models.AddOrder(sub)
		if err != nil {
			c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
			c.ServeJSON()
			c.StopRun()
		}
		c.Data["json"] = Status{ErrCode: POST_OK, Msg: "AddOrders success"}
		c.ServeJSON()
		c.StopRun()
	} else {
		c.Data["json"] = Status{ErrCode: POST_ERR, Msg: "Error"}
		c.ServeJSON()
		c.StopRun()
	}
}
